package co.gov.medellin.ssti.sub.validator;

import java.sql.*;

import co.gov.medellin.ssti.sub.common.ConnectionFactory;
import co.gov.medellin.ssti.sub.common.excepcion.SUBException;

public class Validaciones {

	private ConnectionFactory cf;
	private Connection conn = null;

	public Validaciones(ConnectionFactory cf) {
		this.cf = cf;
		try {
			this.conn = this.cf.getConnection();
		} catch (SQLException e) {
			SUBException.logDebugError("", e);
		}

	}// Validaciones

	public boolean ValidaUsu(String strusuario) {

		String strSql = "SELECT nombre FROM paoyer.tcs_usuario WHERE codigo = ? ";
		String strPar[] = { strusuario };
		ResultSet rs = null;

		try {
			rs = cf.getRecordsRset(strSql, strPar, conn);
			if (rs.next()) {
				return true;
			} else {
				return false;
			}

		} catch (SQLException sqle) {
			return false;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					SUBException.logDebugError("", e);
				}
			}
		}

	}

	public boolean validaActProg() {

		String strSql = "SELECT nombre, codigo FROM tcs_ben_programa WHERE vigencia = 'I' OR SYSDATE > HASTA_VIGENCIA";
		// String strPar[] = {};
		ResultSet rs = null;

		try {
			rs = cf.getRecordsRset(strSql, null, conn);
			if (rs.next()) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException sqle) {
			SUBException.logDebugError("", sqle);
			return false;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					SUBException.logDebugError("", e);
				}
			}
		}
	}

	public boolean validaActSub() {

		String strSql = "SELECT nombre, codigo FROM tcs_ben_subprograma WHERE vigencia = 'I' ";
		// String strPar[] = {};
		ResultSet rs = null;

		try {
			rs = cf.getRecordsRset(strSql, null, conn);
			if (rs.next()) {
				return true;
			} else {
				return false;
			}

		} catch (SQLException sqle) {
			return false;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					SUBException.logDebugError("", e);
				}
			}
		}
	}

	public boolean validasubprg(String strsubprg) {
		String strSql = "SELECT nombre FROM tcs_ben_subprograma WHERE UPPER(nombre) like UPPER (?) ";
		String strsub = strsubprg.trim();
		String strPar[] = { strsub };
		ResultSet rs = null;
		try {
			rs = cf.getRecordsRset(strSql, strPar, conn);
			if (rs.next()) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException sqle) {
			return false;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					SUBException.logDebugError("", e);
				}
			}
		}
	}

	public boolean validaprog(String strprg) {
		String strSql = "SELECT nombre FROM tcs_ben_programa WHERE UPPER(nombre) LIKE UPPER(?) ";
		String strsub = strprg.trim();
		String strPar[] = { strsub };
		ResultSet rs = null;
		try {
			rs = cf.getRecordsRset(strSql, strPar, conn);
			if (rs.next()) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException sqle) {
			return false;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					SUBException.logDebugError("", e);
				}
			}
		}

	}

	public boolean validasede(String strsede) {
		String strSql = "SELECT nombre FROM tcs_ben_sede WHERE UPPER(nombre) = UPPER(?) ";
		// String strsub = "%"+strprg+"%";
		String strPar[] = { strsede };
		ResultSet rs = null;
		try {
			rs = cf.getRecordsRset(strSql, strPar, conn);
			if (rs.next()) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException sqle) {
			return false;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					SUBException.logDebugError("", e);
				}
			}
		}

	}

	public boolean validaUsuadmin(String strusu) {
		String strSql = "SELECT nombre FROM paoyer.tcs_usuario WHERE nombre LIKE UPPER(?) AND Fcs_Val_Vigencia(VIGENCIA,DESDE_VIGENCIA,HASTA_VIGENCIA) = 'S' ";
		String strPar[] = { strusu };
		ResultSet rs = null;
		try {
			rs = cf.getRecordsRset(strSql, strPar, conn);
			if (rs.next()) {
				return true;
			} else {
				return false;
			}

		} catch (SQLException sqle) {
			return false;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					SUBException.logDebugError("", e);
				}
			}
		}
	}

	public boolean ValidaDias(String fecha1, String fecha2, String strdias) {
		String strfecha1[] = fecha1.split("-");
		String strfecha2[] = fecha2.split("-");

		int anos = (Integer.valueOf(strfecha1[0]).intValue() - Integer.valueOf(
				strfecha2[0]).intValue()) * 365;
		int meses = (Integer.valueOf(strfecha1[1]).intValue() - Integer
				.valueOf(strfecha2[1]).intValue()) * 365;
		int dias = Integer.valueOf(strfecha1[2]).intValue()
				- Integer.valueOf(strfecha2[2]).intValue();

		int total = (anos + meses + dias) * (-1);
		int diasreut = Integer.parseInt(strdias.trim());

		if (diasreut <= total) {
			return true;
		} else {
			return false;
		}
	}

	public Connection getConn() {
		return conn;
	}

}
